import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import org.junit.jupiter.api.Test;

import java.util.HashMap;

public class HttpDemo {

    @Test
    void testHttpGet() {
        try {
            String result = HttpUtil.get("https://www.baidu.com");
            System.out.println(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    void testHttpPostBrForm() {
        String account = "110";
        String apiCode = "119";
        try {
            //x-www-form-urlencoded
            HashMap<String, Object> formParamMap = new HashMap<>();
            formParamMap.put("userName", account);
            formParamMap.put("password", account);
            formParamMap.put("apiCode", apiCode);
            String result = HttpUtil.post("https://sandbox-api.100credit.cn/bankServer2/user/login.action",
                    formParamMap);
            System.out.println(result);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //x-www-form-urlencoded
    @Test
    void testHttpPostLocalForm() {
        try {
            HashMap<String, Object> paramMap = new HashMap<>();
            paramMap.put("id", 11);
            paramMap.put("name", "gin");
            String result = HttpUtil.post("localhost:8080/userForm", paramMap);
            System.out.println(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // raw json
    @Test
    void testHttpPostLocalJson() {
        try {
            HashMap<String, Object> paramMap = new HashMap<>();
            paramMap.put("id", 11);
            paramMap.put("name", "gin");
            //String result = HttpUtil.post("localhost:8080/userForm", JSONObject.toJSONString(paramMap));
            String result = HttpUtil.createPost("localhost:8080/userJson")
                    .contentType("application/json")
                    .body(JSONObject.toJSONString(paramMap)).execute().body();
            System.out.println(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
